<?php

class Retailer_Backend_Block_Adminhtml_Wholesale_Grid extends Mage_Adminhtml_Block_Widget_Grid
{
  public function __construct()
  {
      parent::__construct();
      $this->setId('wholesaleGrid');
      $this->setDefaultSort('entity_id');
      $this->setDefaultDir('ASC');
      $this->setSaveParametersInSession(true);
  }

  protected function _prepareCollection()
  {
      $user = Mage::getSingleton('admin/session')->getUser();
      //$store = $this->_getStore($user->getId());
      $link = Mage::getModel('backend/backend')->load($user->getUserId());
      $customer = Mage::getModel('customer/customer')->load($link->getCustomerId());
      //$collection = Mage::getModel('sales/order')->getCollection();
      $collection = Mage::getResourceModel('sales/order_collection')
            ->addAttributeToSelect('*')
            //->joinAttribute('customer_email', 'order/customer_email', 'entity_id', null, 'left')
            ->joinAttribute('billing_firstname', 'order_address/firstname', 'billing_address_id', null, 'left')
            ->joinAttribute('billing_lastname', 'order_address/lastname', 'billing_address_id', null, 'left')
            ->joinAttribute('shipping_firstname', 'order_address/firstname', 'shipping_address_id', null, 'left')
            ->joinAttribute('shipping_lastname', 'order_address/lastname', 'shipping_address_id', null, 'left')
            ->addExpressionAttributeToSelect('billing_name',
                'CONCAT({{billing_firstname}}, " ", {{billing_lastname}})',
                array('billing_firstname', 'billing_lastname'))
            ->addExpressionAttributeToSelect('shipping_name',
                'CONCAT({{shipping_firstname}}, " ", {{shipping_lastname}})',
                array('shipping_firstname', 'shipping_lastname'));
      $collection->addFilter('customer_id',$customer->getId()) ;
      $collection->addFilter('store_id',$link->getStoreId()) ;
      
      //$collection->addStoreFilter($store);
      //$collection->joinAttribute('price', 'catalog_product/price', 'entity_id', null, 'left', $store->getId());
      $this->setCollection($collection);
      parent::_prepareCollection();
      //$this->getCollection()->addWebsiteNamesToResult();
      //$this->_setFilterValues(array('websites'=>'1'));
      //return $this;
  }

  protected function _getStore($storeId)
  {
        return Mage::app()->getStore((int)$storeId);
  }

  protected function _prepareColumns()
  {
      $this->addColumn('real_order_id', array(
          'header'    => Mage::helper('backend')->__('Order Number'),
          'align'     =>'right',
          'width'     =>'100',
          'index'     => 'increment_id',
      ));

      $this->addColumn('billing_name', array(
          'header'    => Mage::helper('backend')->__('Billed To'),
          'align'     =>'left',
          'index'     => 'billing_name',
      ));
      $this->addColumn('shipping_name', array(
          'header'    => Mage::helper('backend')->__('Shipped To'),
          'align'     =>'left',
          'index'     => 'shipping_name',
      ));
      $this->addColumn('customer_email', array(
          'header'    => Mage::helper('backend')->__('Customer Email'),
          'align'     =>'left',
          'index'     => 'customer_email',
      ));
      $this->addColumn('grand_total', array(
            'header' => Mage::helper('sales')->__('Grand Total (Purchased)'),
            'index' => 'grand_total',
            'type'  => 'currency',
            'currency' => 'order_currency_code',
      ));
        $this->addColumn('action',
            array(
                'header'    =>  Mage::helper('backend')->__('Action'),
                'width'     => '100',
                'type'      => 'action',
                'getter'    => 'getId',
                'actions'   => array(
                    array(
                        'caption'   => Mage::helper('backend')->__('View'),
                        'url'       => array('base'=> '*/*/edit'),
                        'field'     => 'id'
                    )
                ),
                'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',
                'is_system' => true,
        ));
		
		//$this->addExportType('*/*/exportCsv', Mage::helper('backend')->__('CSV'));
		//$this->addExportType('*/*/exportXml', Mage::helper('backend')->__('XML'));
	  
      return parent::_prepareColumns();
  }

    protected function _prepareMassaction()
    {
        $this->setMassactionIdField('backend_id');
        $this->getMassactionBlock()->setFormFieldName('backend');

//        $this->getMassactionBlock()->addItem('delete', array(
//             'label'    => Mage::helper('backend')->__('Delete'),
//             'url'      => $this->getUrl('*/*/massDelete'),
//             'confirm'  => Mage::helper('backend')->__('Are you sure?')
//        ));

        $statuses = Mage::getSingleton('backend/status')->getOptionArray();

        array_unshift($statuses, array('label'=>'', 'value'=>''));
//        $this->getMassactionBlock()->addItem('status', array(
//             'label'=> Mage::helper('backend')->__('Change status'),
//             'url'  => $this->getUrl('*/*/massStatus', array('_current'=>true)),
//             'additional' => array(
//                    'visibility' => array(
//                         'name' => 'status',
//                         'type' => 'select',
//                         'class' => 'required-entry',
//                         'label' => Mage::helper('backend')->__('Status'),
//                         'values' => $statuses
//                     )
//             )
//        ));
        return $this;
    }

  public function getRowUrl($row)
  {
      return $this->getUrl('*/*/edit', array('id' => $row->getId()));
  }

}